package edu.friday.repository.custom.impl;

import edu.friday.repository.custom.SysUserCustomRepository;
import edu.friday.utils.SqlUtil;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Query;


public class SysUserCustomRepositoryImpl implements SysUserCustomRepository {
    @PersistenceContext
    private EntityManager em;

    @Override
    public int batchInserUserRole(Long[] userIds, Long[] roleIds) {
        int length=Math.min(userIds.length, roleIds.length);
        StringBuffer sql=new StringBuffer();
        sql.append(" insert into sys_user_role (user_id, role_id) values ");
        sql.append(SqlUtil.getBatchInsertSqlStr(length,2));
        Query query=em.createNativeQuery(sql.toString());
        int paramIndex =1;
        for(int i=0;i<userIds.length;i++){
            query.setParameter(paramIndex++,userIds[i]);
            query.setParameter(paramIndex++,roleIds[i]);
        }
        return query.executeUpdate();
    }
}
